<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use App\Models\Video;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class VideoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
          $video = Video::where('type','=','0')->paginate(9);
          $expert = Expert::paginate(9);
          return view('front.previous-videos')->with('video', $video)->with('expert', $expert);


    }
    public function el(){
        $now_time= time();
        $now_date= date('Y-m-d H:i:s',$now_time);
        $oVideos=Video::where('type','=',0)->orderby('begin','desc')->get();
        $qVideo=Video::where('begin', '>', $now_date)->where('type','=',1)->orderby('begin')->get();
        $zVideo=Video::where('begin', '<=', $now_date)->where('end', '>', $now_date)->where('type','=', 1)->orderby('begin')->get();
        return view('front.expert-lecture')->with('qVideo',$qVideo)->with('zVideo',$zVideo)->with('oVideos',$oVideos);


    }
    public function shouye()
    {
        $now_time = time();
        $now_date = date('Y-m-d H:i:s', $now_time);
        $oVideos = Video::where('type', '=', 0)->orderby('begin', 'desc')->get();//从Video模型中的video表中筛选类型为0的视频，按照begin字段降序排列，get是执行查询并返回结果集，将结果集赋值给 $oVideos 变量
        $qVideo = Video::where('begin', '>', $now_date)->where('type', '=', 1)->orderby('begin')->get();//从Video模型中的video表中筛选时间在当前时间之后，类型为1的视频，按照直播开始时间升序排列，get是执行查询并返回结果集，将结果集赋值给 $oVideos 变量
        $zVideo = Video::where('begin', '<=', $now_date)->where('end', '>', $now_date)->where('type', '=', 1)->orderby('begin')->get();//从 Video 模型中查询类型为 1 且开始时间在当前时间之前且结束时间在当前时间之后的视频，并按照开始时间升序排列
        return view('front.index')->with('qVideo', $qVideo)->with('zVideo', $zVideo)->with('oVideos', $oVideos);//将查询结果传递给front.index视图模板,将 $qVideo 变量传递给视图，并且使用 qVideo 作为在视图中引用该变量的名称
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $expert = Expert::all();
        return view('admin.video-add-list')->with('expert', $expert);
        //
    }

    public function savephoto($maxFileSize = 2000000)
    {
        $allow_type = ["image/jpeg", "image/gif", "image/png"];
        if (!in_array($_FILES['upload_file']['type'], $allow_type)) {
            $msg = '图片格式错误';
            return json_encode(['success' => false, 'msg' => $msg]);
        } elseif ($_FILES['upload_file']['size'] > $maxFileSize) {
            $msg = '图片大小不符合要求';
            return json_encode(['success' => false, 'msg' => $msg]);
        } elseif ($_FILES['upload_file']['error'] != 0) {
            $msg = '上传错误';
            return json_encode(['success' => false, 'msg' => $msg]);
        } else {
            $src_image = $_FILES['upload_file']['tmp_name'];
            $size = getimagesize($_FILES['upload_file']['tmp_name']);
            $src_width = $size[0];
            $src_height = $size[1];
            $tar_width = 264;
            $tar_height = 155;
            $tar_thumb = imagecreatetruecolor($tar_width, $tar_height);
            switch ($_FILES['upload_file']['type']) {
                case 'image/jpeg':
                    $src_thumb = imagecreatefromjpeg($src_image);
                    break;
                case 'image/gif':
                    $src_thumb = imagecreatefromgif($src_image);
                    break;
                case 'image/png':
                    $src_thumb = imagecreatefrompng($src_image);
                    break;
                default:
                    $msg = '不支持的图片格式';
                    return json_encode(['success' => false, 'msg' => $msg]);
            }
            imagecopyresized($tar_thumb, $src_thumb, 0, 0, 0, 0, $tar_width, $tar_height, $src_width, $src_height);
            $path = './uploads/video_img/' . $_FILES['upload_file']['name'];
            imagejpeg($tar_thumb, $path, 80);
            imagedestroy($src_thumb);
            imagedestroy($tar_thumb);
            return json_encode(['success' => true, 'img_thumb' => '/uploads/video_img/' . $_FILES['upload_file']['name']]);
        }
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->all();
        $rules = [
            'title' => 'required',
            'url' => 'required',
            'description'=>'required',
            'begin'=>'required',
            'end'=>'required',
        ];
        $message=[
        ];
        $validator = Validator::make($data, $rules,$message);
        if ($validator->fails()) {
            return view('admin.videoaddfail');
        }
        $type = $request->input('q');
        $data["type"] = $type;
            if (isset($data['img_thumb'])) {
                $data["thumb"] = $data['img_thumb'];
            } else {
                $data["thumb"] = '/assets/test_images/1.jpg';
            }
            $data['content'] = $data['description'];
            $expert = Expert::find($data['doc_id'][0]);
            $data['expert'] = $expert->name;
            $video = Video::create($data);
            foreach ($data['doc_id'] as $v) {
                $video->connection()->attach((int)$v);
            }
            return view('admin.videoaddsuccess');

    }


    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */

    public function show($id)
    {
        $othervideo=Video::where('type','=','0')->where('id','!=',$id)->orderby('id','desc')->take(1)->get();
        $currentvideo = Video::find($id);
        return view('front.previous-videos-details')->with('othervideo', $othervideo)->with('currentvideo', $currentvideo);
    }
    public function adminlist()
    {
        $video = Video::paginate(9);
        return view('admin.video-show-list')->with('video', $video);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $video = Video::find($id);
        $expert = Expert::all();
        return view('admin.video-edit-list')->with('video', $video)->with('expert', $expert);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $video = Video::find($id);
        $date = $request->input();
        $type1 = $request->input('q1');
        $type2 = $request->input('q2');
        if ($type1 > $type2) {
            $type = 0;
        } else(
        $type = 1
        );
        $date["type"] = $type;
        if (isset($date['img_thumb'])) {
            $date["thumb"] = $date['img_thumb'];
        } else {
            $date["thumb"] = '/assets/test_images/1.jpg';
        }
        $date['content'] = $request->input('description');
        $expert = Expert::find($date['doc_id'][0]);
        $date['expert'] = $expert->name;
        $video->update($date);
        foreach ($date['doc_id'] as $v) {
            $video->connection()->attach((int)$v);
        }
        return view('admin.videoeditsuccess');



    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        $video = Video::find($id);
        $video->delete();
        return view('admin.videodeletesuccess');
        //
    }
}

